package com.sys.web.head.controller;

import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.sys.common.controller.BaseController;
import com.sys.common.exception.GoodsStockException;
import com.sys.common.pojo.ApiResponse;
import com.sys.common.pojo.ApiResponseEnum;
import com.sys.common.util.ConstatFinalUtil;
import com.sys.common.util.PageInfoUtil;
import com.sys.goods.service.IGoodsDbService;
import com.sys.orders.pojo.AOrders;
import com.sys.orders.pojo.AOrdersEnum;
import com.sys.orders.pojo.AOrdersGoods;
import com.sys.orders.service.IOrdersDbService;
import com.sys.users.pojo.ACart;
import com.sys.users.pojo.AUsers;
import com.sys.users.pojo.AUsersEnum;
import com.sys.users.service.IUsersDbService;

/**
 * 订单操作
 * 
 * @author 
 */
@Controller
@RequestMapping(value = "/head/orders",produces = "text/html;charset=UTF-8")
public class OrdersHeadController extends BaseController
{
	@Autowired
	private IOrdersDbService ordersDbService;
	@Autowired
	private IGoodsDbService goodsDbService ; 
	@Autowired
	private IUsersDbService usersDbService ; 
	
	
	/**
	 * 订单列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/ordersList")
	public String ordersList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		ConstatFinalUtil.SYS_LOGGER.info("=====ordersList=========");
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		condMap.put("usersId", users.getId());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AOrders> apiResponse = this.ordersDbService.findCondListOrdersService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/orders/ordersList";
	}

	/**
	 * 打开下单页面
	 * @param request
	 * @param response
	 */
	@RequestMapping("/ordersInsert")
	public String ordersInsert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("usersId", users.getId());
		condMap.put("status", AUsersEnum.STATUS_ENABLE.getStatus());
		condMap.put("extend", "true");
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ACart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		
		if(updateFlag == null || "".equalsIgnoreCase(updateFlag))
		{
			/* 清除信息 */
			apiResponse.clearInfo();
		}
		/* 保留原来的信息 */
		JSONObject responseJSON = this.souResponse(request, apiResponse.toJSON());
		
		/* 将结果存储到Request中 */
		request.setAttribute("response", responseJSON);
		
		/* 页面跳转 */
		return "/head/orders/ordersInsert" ; 
	}
	
	/**
	 * 下单提交
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/ordersInsertSubmit")
	public String ordersInsertSubmit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		AUsers users = (AUsers) this.findObjfromSession(request, "users");
		/*========查询购物车=========*/
		/* 分页信息 */
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		/* 查询我的,查询启用的购物车 */
		condMap.put("usersId", users.getId());
		condMap.put("status", AUsersEnum.STATUS_ENABLE.getStatus());
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<ACart> apiResponse = this.usersDbService.findCondListCartService(null, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 接收必要信息 */
		String receName = request.getParameter("receName");
		String recePhone = request.getParameter("recePhone");
		String receAddress = request.getParameter("receAddress");
		String content = request.getParameter("content");
		
		/* 创建对象 */
		AOrders orders = new AOrders();
		orders.setUsersId(users.getId());
		orders.setReceName(receName);
		orders.setRecePhone(recePhone);
		orders.setReceAddress(receAddress);
		orders.setContent(content);
		
		/* 设置购物车 */
		List<ACart> cartList = apiResponse.getDataListJava();
		orders.setCartList(cartList);
		
		ApiResponse<Object> apiResponseDb = new ApiResponse<Object>();
		try {
			apiResponseDb = this.ordersDbService.saveOneOrdersService(orders);
			if(apiResponseDb.getCode() == ApiResponseEnum.STATUS_SUCCESS.getStatus())
			{
				return "redirect:/head/orders/ordersList" ; 
			}
		} catch (GoodsStockException e) {
			apiResponseDb.setCode(ApiResponseEnum.INFO_STOCK_NUM.getStatus());
			apiResponseDb.setInfo(ConstatFinalUtil.INFO_JSON.getString(apiResponseDb.getCode() + ""), Collections.EMPTY_MAP);
		}
		request.setAttribute("response", apiResponseDb.toJSON());
		return this.ordersInsert(request, response);
	}

	/**
	 * 订单更新
	 * @param request
	 * @param response
	 */
	@RequestMapping("/ordersUpdate")
	public String ordersUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<AOrders> apiResponse = this.ordersDbService.findOneOrdersService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 同时查询出订单商品信息 */
		AOrders orders = apiResponse.getDataOneJava();
		condMap.clear();
		condMap.put("ordersId", orders.getId() + "");
		condMap.put("extend", "true");
		ApiResponse<AOrdersGoods> ordersGoodsApiResponse = this.ordersDbService.findCondListOrdersGoodsService(null, condMap);
		request.setAttribute("ordersGoodsResponse", ordersGoodsApiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/head/orders/ordersUpdate" ; 
		}else if("pay".equalsIgnoreCase(operType))
		{
			return "/head/orders/ordersPay" ; 
		}
		/* 页面跳转 */
		return "/head/orders/ordersInfo" ; 
	}
	
	/**
	 * 订单更新提交
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/ordersUpdateSubmit")
	public String ordersUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("=======ordersUpdateSubmit========");
		ApiResponse<Object> apiResponse = new ApiResponse<Object>();
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<AOrders> apiDbResponse = this.ordersDbService.findOneOrdersService(condMap);
		request.setAttribute("response", apiDbResponse.toJSON());
		
		/* 同时查询出订单商品信息 */
		AOrders orders = apiDbResponse.getDataOneJava();
		condMap.clear();
		condMap.put("ordersId", orders.getId() + "");
		condMap.put("extend", "true");
		ApiResponse<AOrdersGoods> ordersGoodsApiResponse = this.ordersDbService.findCondListOrdersGoodsService(null, condMap);
		request.setAttribute("ordersGoodsResponse", ordersGoodsApiResponse.toJSON());
		
		boolean flag = false ; 
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType)) {
			String status = request.getParameter("status");
			orders.setStatus(Byte.valueOf(status));
			flag = true ; 
		}else if("pay".equalsIgnoreCase(operType)) {
			String payType = request.getParameter("payType");
			orders.setPayType(Byte.valueOf(payType));
			orders.setPayTime(new Date());
			orders.setStatus(AOrdersEnum.STATUS_YFK.getStatus());
			flag = true ; 
		}
		/* 接收参数 */
		if(flag) {
			apiResponse = this.ordersDbService.updateOneOrdersService(orders);
			ConstatFinalUtil.SYS_LOGGER.info(apiResponse.toJSON().toJSONString());
		}
		request.setAttribute("response", apiDbResponse);
		return "redirect:/head/orders/ordersList";
	}

	/**
	 * 订单商品列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	@RequestMapping("/ordersGoodsList")
	public String ordersGoodsList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 分页信息 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		/* 搜索条件,生成公共的搜索条件 */
		Map<String, Object> condMap = this.proccedSearch(request);
		String ordersId = request.getParameter("ordersId");
		if(ordersId == null)
		{
			ordersId = "" ; 
		}
		request.setAttribute("ordersId", ordersId);
		condMap.put("ordersId", ordersId);
		
		/* 查询数据库
		 * 所有的数据以JSON的形式返回
		 *  */
		ApiResponse<AOrdersGoods> apiResponse = this.ordersDbService.findCondListOrdersGoodsService(pageInfoUtil, condMap);
		/* 将结果存储到Request中 */
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 页面跳转 */
		return "/head/orders/ordersGoodsList" ; 
	}

	/**
	 * 订单商品更新
	 * @param request
	 * @param response
	 */
	@RequestMapping("/ordersGoodsUpdate")
	public String ordersGoodsUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
	{
		/* 准备路径 */
		Map<String, Object> condMap = new HashMap<String, Object>();
		
		/* 按照id查询 */
		String id = request.getParameter("id");
		condMap.put("id", id);
		condMap.put("extend", "true");
		ApiResponse<AOrdersGoods> apiResponse = this.ordersDbService.findOneOrdersGoodsService(condMap);
		request.setAttribute("response", apiResponse.toJSON());
		
		/* 根据不同的判断处理不同的类型 */
		String operType = request.getParameter("operType");
		if("update".equalsIgnoreCase(operType))
		{
			return "/head/orders/ordersGoodsUpdate" ; 
		}
		/* 页面跳转 */
		return "/head/orders/ordersGoodsInfo" ; 
	}
}
